iT邦幫忙

2025 iThome 鐵人賽

DAY 1
0
生成式 AI

AI LeetCode 助教:30 天打造智慧刷題系統系列 第 1

Day 1 — 專案啟動與雛形建立:AI LeetCode 助教

  • 分享至 

  • xImage
  •  

前言

在學習演算法的過程中,我們時常遇到找不到符合自己程度的題目練習,導致大家不知道自己的實作能力,也不確定是否真正了解演算法理論,今年的 iThome 鐵人賽,我將開發一個實際能讓大家用的AI平台——AI LeetCode助教。
我們的目標很明確:幫助使用者透過生成式 AI 在 LeetCode 上更有效率地學習演算法,提供程度判斷、題目推薦、解題提示與錯誤分析,最終讓這個平台對所有程式學習者開放使用。

今日目標

  • 建立專案結構
  • 建立後端 API(FastAPI)與前端介面(Streamlit)
  • 設定 SQLite 資料庫與題目資料表
  • 寫一個 Mock 題庫並能在前端顯示
  • 完成基本啟動流程

系統架構雛形

  • 後端:FastAPI 提供 API,負責題庫資料與未來的AI分析功能
  • 前端:Streamlit 做互動介面,方便快速開發
  • 資料庫:SQLite 存題目與使用者紀錄
  • 部署:本地端先跑通,之後再雲端部署
    分工:
  • 後端 API:提供 /health 與 /problems 兩個端點
  • 資料庫:題庫表(Problem)
  • 前端頁面:顯示系統狀態與題目列表

開發步驟

  1. 建立專案
ai-leetcode-tutor/
├─ backend/        # FastAPI 後端
├─ frontend/       # Streamlit 前端
└─ README.md
  1. 安裝套件

前端:

pip install streamlit requests

後端:

pip install fastapi uvicorn[standard] pydantic SQLAlchemy python-dotenv
  1. 後端 API — FastAPI
    建立 /health 確認 API 正常、/problems 提供題目列表(目前使用 mock 資料)。
# backend/app.py
from fastapi import FastAPI
from .database import Base, engine
from .routers import problems

app = FastAPI(title="AI LeetCode Tutor API")
Base.metadata.create_all(bind=engine)

@app.get("/health")
def health():
    return {"status": "ok"}

app.include_router(problems.router)
  1. 建立資料庫與 Mock 題庫
    使用 SQLite 建立Problem資料表,並插入3筆範例題。
MOCK = [
    {"slug":"two-sum","title":"Two Sum","difficulty":"Easy","topic":"Array"},
    {"slug":"longest-substring","title":"Longest Substring Without Repeating Characters","difficulty":"Medium","topic":"HashMap"},
    {"slug":"median-of-two-sorted-arrays","title":"Median of Two Sorted Arrays","difficulty":"Hard","topic":"Binary Search"}
]
  1. 前端 UI — Streamlit
# frontend/app.py
import streamlit as st
import requests
API_BASE = "http://127.0.0.1:8000"

st.title("AI LeetCode 助教(Day 1 雛形)")
st.subheader("系統狀態")
st.write(requests.get(f"{API_BASE}/health").json())

st.subheader("題目列表")
for p in requests.get(f"{API_BASE}/problems").json():
    st.write(f"[{p['difficulty']}] {p['title']} ({p['topic']})")

今日成果

  1. 啟動後端:
cd backend
uvicorn app:app --reload
  1. 初始化題庫:
python seed_mock.py
  1. 啟動前端:
cd frontend
streamlit run app.py

今日結論

第一天介紹了這個專案動機以及目標,並且完成了整個專案的基礎架構,雖然功能很簡單,但今天已經實作出:

  • 前後端可互通的雛形
  • 資料庫連線與題庫表
  • 第一版可運作的 UI

下一篇
Day 2:資料模型擴充 + 篩選查詢 + 基礎 CRUD
系列文
AI LeetCode 助教:30 天打造智慧刷題系統4
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言